package com.neusoft.service.Impl;

import java.io.Console;
import java.sql.Date;
import java.time.LocalDate;
import java.time.Period;
import java.time.Year;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.mapper.VacationMapper;
import com.neusoft.po.VacationPO;
import com.neusoft.po.Vacation_TypePO;
import com.neusoft.service.VacationService;
import com.neusoft.vo.SdayVO;
import com.neusoft.vo.VacationVO;
import com.neusoft.vo.VacationVVO;
import com.neusoft.vo.Vacation_TypeVO;
import com.neusoft.vo.VacationagreeVO;
import com.neusoft.vo.VacationckVO;
import com.neusoft.vo.Vacationcvo;
import com.neusoft.vo.Vacationlistvo;

@Service
public class VacationServicempl implements VacationService {
	@Autowired
	private VacationMapper vm;
	//新增假期类型
	@Override
	public void insertvacation(VacationVVO po) {
		// TODO Auto-generated method stub
		vm.inertVacation(po);
	}

//	if (po.getLimitt()==0) {
//		vm.inertVacation(po);
//	}
//	else {
//		vm.inertVacation(po);
//		po.setVacation_id(vm.getid());
//		List<Integer> a = vm.getempno();
//		for (Integer integer : a) {
//			po.setEmpno(integer);
//			vm.inertVacation2(po);
//		}
//		
//	}
	//更新假期类型
	@Override
	public void updateVacation(Vacationcvo po) {
		// TODO Auto-generated method stub
		vm.updateVacation(po);
	}

	// 删除假期类型
	@Override
	public void delateVacation(Integer vacation_id) {
		// TODO Auto-generated method stub
		vm.delateVacation(vacation_id);
	}

	// 新增休假记录
	@Override
	public void addVacation(VacationVO vo) {
		// TODO Auto-generated method stub
		vm.addVacation(vo);
	}


	// 更新年假
	@Override
	public void upvacation() {
		// TODO Auto-generated method stub
		List<Integer> a = vm.getempno();
		for (Integer empno : a) {
			Vacation_TypeVO vo = new Vacation_TypeVO();
			Calendar cal = Calendar.getInstance();
			Calendar cal2 = Calendar.getInstance();
			cal2.setTime(vm.getdata(empno));
			vo.setEmpno(empno);
			Integer yaer = cal.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
			vo.setYear(cal.get(Calendar.YEAR));
			if (yaer < 1) {
				vo.setDays(0);
			} else if (1 <= yaer && yaer < 10) {
				vo.setDays(5);
			} else if (10 <= yaer && yaer < 20) {
				vo.setDays(10);
			} else {
				vo.setDays(10);
			}
			vm.inertVacation2(vo);
		}
	}
//自动删除
	@Override
	public void delvacation() {
		// TODO Auto-generated method stub
		Calendar cal = Calendar.getInstance();
		Integer year = cal.get(Calendar.YEAR);
		vm.delavailable_vacation(year);
	}

	//审批通过
	@Override
	public void agree(VacationagreeVO po) {
		// TODO Auto-generated method stub
		vm.agreeavailable_record(po.getRecord_id());
		// 扣假期记得考虑冒漾问题
		if(po.getRecord_id()!=0&&po.getVacation_id()!=1) {
			//如果是有天数限制的假期需要特殊处理
			vm.upavailable_vacation(po);
		}
		if(po.getVacation_id()==1) {
			//如果是有天数限制的假期需要特殊处理
			for (int i = 0;  i < po.getDuration(); i++) {
			vm.upavailable_vacation2(po);
			}
		}
	}
	//审批不通过
	@Override
	public void refuse(Integer record_id) {
		// TODO Auto-generated method stub
		vm.noavailable_record(record_id);
	}
	//获得请假类型列表
	@Override
	public List<Vacation_TypePO> list() {
		// TODO Auto-generated method stub
		return vm.getvlist();
	}
	//获得个人假期记录
	@Override
	public List<Vacationlistvo> selectList(Integer empno) {
		// TODO Auto-generated method stub
		List<VacationPO> a =vm.selectlist(empno);
		List<Vacationlistvo> d = new ArrayList<Vacationlistvo>();
		for (VacationPO vacationPO : a) {
			Vacationlistvo vo =new Vacationlistvo();
			vo.setEname(vm.getename(empno));
			vo.setVacation_name(vm.getvname(vacationPO.getVacation_id()));
			vo.setDuration(vacationPO.getDuration());
			vo.setStart_date(vacationPO.getStart_date());
			vo.setEnd_date(vacationPO.getEnd_date());
			vo.setState(vacationPO.getState());
			d.add(vo);
		}
		return d;
	}
	
	//获得审批列表
	@Override
	public List<VacationckVO> selectList2(Integer deptno) {
		// TODO Auto-generated method stub
		List<Integer> b = vm.getempnon(deptno);
		List<VacationckVO> d =new ArrayList<VacationckVO>();
		for (Integer no : b) {
			for (VacationPO po : vm.selectlistt(no)) {
				VacationckVO vo =new VacationckVO();
				vo.setEmpno(no);
				vo.setRecord_id(po.getRecord_id());
				vo.setVacation_id(po.getVacation_id());
				vo.setEname(vm.getename(no));
				vo.setVacation_name(vm.getvname(po.getVacation_id()));
				vo.setDuration(po.getDuration());
				vo.setStart_date(po.getStart_date());
				vo.setEnd_date(po.getEnd_date());
				vo.setState(po.getState());
				vo.setVacation_reason(po.getVacation_reason());
				d.add(vo);
			}	
		}
		
		
		return d;
	}

	@Override
	public Integer selectday(SdayVO vo) {
		// TODO Auto-generated method stub
		
		return vm.selectday(vo);
		
	}
	
}
